package com.taiyuan.zfmvp.JUC.ThreadPool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @param
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption:
 * @Date 2022/10/7
 * @return
 */
public class ThreadPoolDemo2 {

    public static  ExecutorService pool = null;

    public static  void Thread4Use(Runnable runnable){
        //System.out.println("初始化线程池");
       if(pool == null){
           initThreadPool();
       }
        try {
            //System.out.println("开始执行");
            pool.execute(runnable);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //System.out.println("执行完成");
            //pool.shutdown();
        }
    }

    public static  void initThreadPool(){
        System.out.println("初始化线程池");
        pool = new ThreadPoolExecutor(
                20,
                150,
                60L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

    }

    public static  ExecutorService Thread4Use2(){
        ExecutorService poolNew = new ThreadPoolExecutor(
                200,
                300,
                60L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

           return poolNew;

    }

}
